package com.playball.lottery.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

/**
 * 存贮数据到本地SharedPreferences类,如我的关注，热门彩种
 * 
 * @author Administrator
 * 
 */
public class MyPreference {
	private static MyPreference prefer = null;
	private String packName = "";
	private Context app;
	private SharedPreferences settings;
	/** 购买彩种ID */
	public static final String BLOTID = "BLotID";
	/** 红黄牌标记 */
	public static final String SOUNDFLGHH = "soundFlghh";
	/** 进球标记 */
	public static final String SOUNDFLGJQ = "soundFlgjq";

	private MyPreference(Context mv) {
		app = mv;
		packName = mv.getPackageName();
		settings = app.getSharedPreferences(packName, 0);
	}

	public static MyPreference getInstance(Context mv) {
		if (prefer == null) {
			prefer = new MyPreference(mv);
		}
		return prefer;
	}

	/**
	 * 存储彩种成功购买BLotID
	 */
	public void storeBLotID(int lotID) {
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt(BLOTID, lotID);
		editor.commit();
	}

	/**
	 * 读取最新成功购买彩种
	 */
	public int getBLotID() {
		if (settings != null) {
			return settings.getInt(BLOTID, 0);
		}
		return 0;
	}

	/**
	 * 存储彩种成功购买后index
	 */
	public void storeLotBIndex(String lotID, int index) {
		if (index != 0 && !"".equals(index)) {
			SharedPreferences.Editor editor = settings.edit();
			editor.putInt(lotID, index);
			editor.commit();
		}
	}

	/**
	 * 读取彩种代码index
	 */
	public int getLotBIndex(String lotID) {
		if (settings != null) {

			return settings.getInt(lotID, 0);
		}
		return 0;
	}

	/**
	 * 存贮比分进球声音提示
	 */
	public void storeSoundFlgJQ(boolean soundFlg) {
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(SOUNDFLGJQ, soundFlg);
		editor.commit();
	}

	/**
	 * 获取进球声音提示标记
	 * 
	 * @return true:保存比分进球声音提示 false:保存比分进球声音不提示
	 */
	public boolean getSoundFlgJQ() {
		return settings.getBoolean(SOUNDFLGJQ, true);
	}

	/**
	 * 存贮比分红黄牌声音提示
	 */
	public void storeSoundFlgHH(boolean soundFlg) {
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(SOUNDFLGHH, soundFlg);
		editor.commit();
	}

	/**
	 * 获取红黄牌声音提示标记
	 * 
	 * @return true:保存比分声音提示 false:声音不提示
	 */
	public boolean getSoundFlgHH() {
		return settings.getBoolean(SOUNDFLGHH, true);
	}
	
	public void setSSQNotice(boolean isNotice){
		settings.edit().putBoolean(Tag.NOTICE_SSQ, isNotice).commit();
	}
	public boolean isSSQNotice(){
		return settings.getBoolean(Tag.NOTICE_SSQ, true);
	}
	
	public void setDLTNotice(boolean isNotice){
		settings.edit().putBoolean(Tag.NOTICE_DLT, isNotice).commit();
	}
	public boolean isDLTNotice(){
		return settings.getBoolean(Tag.NOTICE_DLT, true);
	}
	
	public void setFC3DNotice(boolean isNotice){
		settings.edit().putBoolean(Tag.NOTICE_FC3D, isNotice).commit();
	}
	public boolean isFC3DNotice(){
		return settings.getBoolean(Tag.NOTICE_FC3D, true);
	}
	
	public void setPL3Notice(boolean isNotice){
		settings.edit().putBoolean(Tag.NOTICE_PL3, isNotice).commit();
	}
	public boolean isPL3Notice(){
		return settings.getBoolean(Tag.NOTICE_PL3, true);
	}
	
	public void setQXCNotice(boolean isNotice){
		settings.edit().putBoolean(Tag.NOTICE_QXC, isNotice).commit();
	}
	public boolean isQXCNotice(){
		return settings.getBoolean(Tag.NOTICE_QXC, true);
	}
	
	public void setQLCNotice(boolean isNotice){
		settings.edit().putBoolean(Tag.NOTICE_QLC, isNotice).commit();
	}
	public boolean isQLCNotice(){
		return settings.getBoolean(Tag.NOTICE_QLC, true);
	}
	
	public void setPL5Notice(boolean isNotice){
		settings.edit().putBoolean(Tag.NOTICE_PL5, isNotice).commit();
	}
	public boolean isPL5Notice(){
		return settings.getBoolean(Tag.NOTICE_PL5, true);
	}
	
	public void setSFCNotice(boolean isNotice){
		settings.edit().putBoolean(Tag.NOTICE_SFC, isNotice).commit();
	}
	public boolean isSFCNotice(){
		return settings.getBoolean(Tag.NOTICE_SFC, true);
	}
	
	public void setUserId(String id){
		settings.edit().putString(Tag.USER_ID, id).commit();
	}
	public String getUserID(){
		return settings.getString(Tag.USER_ID, "0");
	}

	public void setStringArrayPref(Context context, String key, List<String> values) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		SharedPreferences.Editor editor = prefs.edit();
		JSONArray a = new JSONArray();
		for (int i = 0; i < values.size(); i++) {
			a.put(values.get(i));
		}
		if (!values.isEmpty()) {

			editor.putString(key, a.toString());
		} else {
			editor.putString(key, null);
		}
		editor.commit();
	}

	public List<String> getStringArrayPref(Context context, String key) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		String json = prefs.getString(key, null);
		List<String> favoritos = new ArrayList<String>();
		if (json != null) {
			try {
				JSONArray a = new JSONArray(json);
				for (int i = 0; i < a.length(); i++) {
					// int musicaid = a.optInt(i);
					String aid = a.optString(i);
					favoritos.add(aid);
				}
			} catch (JSONException e) {
//				e.printStackTrace();
			}
		}
		return favoritos;
	}

	public void setStringArrayPref2(Context context, String key, List<Map<String, Object>> values) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		SharedPreferences.Editor editor = prefs.edit();
		JSONArray a = new JSONArray();
		for (int i = 0; i < values.size(); i++) {
			a.put(values.get(i));
		}
		if (!values.isEmpty()) {
			editor.putString(key, a.toString());
		} else {
			editor.putString(key, null);
		}
		editor.commit();
	}

	public List<Map<String, Object>> getStringArrayPref2(Context context, String key) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		String json = prefs.getString(key, null);
		List<Map<String, Object>> favoritos = new ArrayList<Map<String, Object>>();
		if (json != null) {
			try {
				JSONArray a = new JSONArray(json);
				for (int i = 0; i < a.length(); i++) {
					Map<String, Object> valueMap = new HashMap<String, Object>();
					String aid = a.optString(i);
					valueMap = StringToMap(aid);
					favoritos.add(valueMap);
				}
			} catch (Exception e) {
//				e.printStackTrace();
			}
		}
		return favoritos;
	}

	public void setStringArrayPreflist2(Context context, String xmlname,String key, List<Map<String, Object>> values) {
		SharedPreferences prefs = context.getSharedPreferences(xmlname, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = prefs.edit();
		JSONArray a = new JSONArray();
		for (int i = 0; i < values.size(); i++) {
			a.put(values.get(i));
		}
		if (!values.isEmpty()) {
			editor.putString(key, a.toString());
		} else {
			editor.putString(key, null);
		}
		editor.commit();
	}

	public List<Map<String, Object>> getStringArrayPreflist2(Context context, String xmlname,String key) {
		SharedPreferences prefs = context.getSharedPreferences(xmlname, Context.MODE_PRIVATE);
		String json = prefs.getString(key, null);
		List<Map<String, Object>> favoritos = new ArrayList<Map<String, Object>>();
		if (json != null) {
			try {
				JSONArray a = new JSONArray(json);
				for (int i = 0; i < a.length(); i++) {
					Map<String, Object> valueMap = new HashMap<String, Object>();
					String aid = a.optString(i);
					valueMap = StringToMap(aid);
					favoritos.add(valueMap);
				}
			} catch (Exception e) {
//				e.printStackTrace();
			}
		}
		return favoritos;
	}
	
	/**
	 * 建议反馈 存储 
	 * @param context
	 * @param xmlname
	 * @param key
	 * @return
	 */
	public List<Map<String, Object>> getStringArraySuggestionPreflist2(Context context, String xmlname,String key) {
		SharedPreferences prefs = context.getSharedPreferences(xmlname, Context.MODE_PRIVATE);
		String json = prefs.getString(key, null);
		List<Map<String, Object>> suggestions = new ArrayList<Map<String, Object>>();
		if (json != null) {
			try {
				JSONArray a = new JSONArray(json);
				for (int i = 0; i < a.length(); i++) {
					Map<String, Object> valueMap = new HashMap<String, Object>();
					String aid = a.optString(i);
					valueMap = StringToMapSuggestion(aid);
					suggestions.add(valueMap);
				}
			} catch (Exception e) {
//				e.printStackTrace();
			}
		}
		return suggestions;
	}
	
	/**
	 * String转换Map
	 * 比分直播
	 * @param mapText
	 *            :需要转换的字符串
	 */
	public static Map<String, Object> StringToMap(String mapText) {
		if (mapText == null || mapText.equals("")) {
			return null;
		}
		String ALL1 = mapText.replace("{", " ");
		String ALL2 = ALL1.replace("}", " ");
		String ALL = ALL2.trim();
		Map<String, Object> map = new HashMap<String, Object>();
		String[] strs = ALL.split(",");
		for (int i = 0; i < strs.length; i++) {
			String[] tmp = strs[i].split("=");
			if ((tmp[0].trim()).equals("i1")) {
				if(tmp.length > 1) {
					String stri1 = tmp[1];
					map.put("i1", stri1);
				} else {
					map.put("i1", "");
				}
			}
			if ((tmp[0].trim()).equals("i2")) {
					if(tmp.length > 1) {
						String stri2 = tmp[1];
						map.put("i2", stri2);
					} else {
						map.put("i2", "");
					}
			}
			if ((tmp[0].trim()).equals("i3")) {
				if(tmp.length > 1) {
					String stri3 = tmp[1];
					map.put("i3", stri3);
				} else {
					map.put("i3", "");
				}
			}
			if ((tmp[0].trim()).equals("i4")) {
				if(tmp.length > 1) {
					String stri4 = tmp[1];
					map.put("i4", stri4);
				} else {
					map.put("i4", "");
				}
			}
			if ((tmp[0].trim()).equals("i5")) {
				if(tmp.length > 1) {
					String stri5 = tmp[1];
					map.put("i5", stri5);
				} else {
					map.put("i5", "");
				}
			}
			if ((tmp[0].trim()).equals("i6")) {
				if(tmp.length > 1) {
					String stri6 = tmp[1];
					map.put("i6", stri6);
				} else {
					map.put("i6", "");
				}
			}
			if ((tmp[0].trim()).equals("i7")) {
				if(tmp.length > 1) {
					String stri7 = tmp[1];
					map.put("i7", stri7);
				} else {
					map.put("i7", "");
				}
			}
			if ((tmp[0].trim()).equals("i8")) {
				if(tmp.length > 1) {
					String stri8 = tmp[1];
					map.put("i8", stri8);
				} else {
					map.put("i8", "");
				}
			}
			if ((tmp[0].trim()).equals("i9")) {
				if(tmp.length > 1) {
					String stri9 = tmp[1];
					map.put("i9", stri9);
				} else {
					map.put("i9", "");
				}
			}
			if ((tmp[0].trim()).equals("i10")) {
				if(tmp.length > 1) {
					String stri10 = tmp[1];
					map.put("i10", stri10);
				} else {
					map.put("i10", "");
				}
			}
			if ((tmp[0].trim()).equals("i11")) {
				if(tmp.length > 1) {
					String stri11 = tmp[1];
					map.put("i11", stri11);
				} else {
					map.put("i11", "");
				}
			}
			if ((tmp[0].trim()).equals("i12")) {
				if(tmp.length > 1) {
					String stri12 = tmp[1];
					map.put("i12", stri12);
				} else {
					map.put("i12", "");
				}
			}
			if ((tmp[0].trim()).equals("i13")) {
				if(tmp.length > 1) {
					String stri13 = tmp[1];
					map.put("i13", stri13);
				} else {
					map.put("i13", "");
				}
			}
			if ((tmp[0].trim()).equals("isCheck")) {
				if(tmp.length > 1) {
					int intCheck = Integer.parseInt(tmp[1]);
					map.put("isCheck", intCheck);
				} else {
					map.put("isCheck", 0);
				}
			}
			// if ((tmp[0].trim()).equals("fustime")) {
			// String fustime = tmp[1];
			// map.put("fustime", fustime);
			// }
		}
		return map;
	}

	/**
	 * String转换Map
	 * 建议反馈 存储 
	 * @param mapText
	 *            :需要转换的字符串
	 */
	public static Map<String, Object> StringToMapSuggestion(String mapText) {
		if (mapText == null || mapText.equals("")) {
			return null;
		}
		String ALL1 = mapText.replace("{", " ");
		String ALL2 = ALL1.replace("}", " ");
		String ALL = ALL2.trim();
		Map<String, Object> map = new HashMap<String, Object>();
		String[] strs = ALL.split(",");
		for (int i = 0; i < strs.length; i++) {
			if ((strs[i].split("=")[0].trim()).equals("time")) {
				String stri1 = strs[i].split("=")[1];
				map.put("time", stri1);
			}
			if ((strs[i].split("=")[0].trim()).equals("content")) {
				String stri2 = strs[i].split("=")[1];
				map.put("content", stri2);
			}
		}
		return map;
	}
}
